<template>
    <div>
        <div style="height: 50px">
            <el-input
                    placeholder="请输入名称"
                    name="roleName"
                    v-model:value="roleName"
                    suffix-icon="el-icon-user"
                    style="width: 200px;padding-right: 5px;"
            >
            </el-input>
            <el-button class="el_margin_lr" type="primary" icon="el-icon-search" @click="search" >搜索</el-button>

            <el-button class="el_margin_lr" type="warning" icon="el-icon-search" @click="reset" >重置</el-button>
        </div>

        <div style="height: 50px">
            <el-button type="primary" @click="title='新增角色',roleForm={},dialogFormVisible=true">新增<i class="el-icon-circle-plus-outline" style="margin-left: 2px"></i></el-button>
            <el-button type="danger" @click="batchDeleteUsers">批量删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>

        </div>

        <el-table :data="tableData" header-cell-class-name="headerTableBg" @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column prop="id" label="ID" width="140"></el-table-column>
            <el-table-column prop="name" label="角色名" width="180"></el-table-column>
            <el-table-column prop="description" label="角色描述" ></el-table-column>
            <el-table-column prop="roleFlag" label="角色标识" ></el-table-column>

            <el-table-column prop="operation" label="操作">
                <template slot-scope="scope">
                    <el-button v-if="roleFlag==='ROLE_ADMIN'"
                               type="primary" @click="assignPermission(scope.row.id,scope.row.roleFlag)">
                            分配权限
                        <i class="el-icon-circle-plus-outline" style="margin-left: 2px"></i></el-button>
                    <el-button type="success" @click="toEditRole(scope.row)" >编辑<i class="el-icon-edit" style="margin-left: 2px"></i></el-button>

                    <el-popconfirm
                            class="el_margin_lr"
                            confirm-button-text='确定'
                            cancel-button-text='取消'
                            icon="el-icon-info"
                            icon-color="red"
                            title="确认删除该用户吗？"
                            @confirm="deleteRole(scope.row.id)">
                        <el-button  type="danger" slot="reference">删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>

        </el-table>
        <div >
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageIndex"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="totalNum">
            </el-pagination>
        </div>

        <el-dialog :title="title" style="text-align: center;" :visible.sync="dialogFormVisible" width="30%">
            <div v-if="title=='修改角色'">
                <el-form label-width=80px >
                    <el-form-item label="角色名" >
                        <el-input v-model="roleForm.name" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="角色描述" >
                        <el-input v-model="roleForm.description" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="唯一标识" >
                        <el-input v-model="roleForm.roleFlag" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addRole">确 定</el-button>
                </div>
            </div>
            <div v-else-if="title=='新增角色'">
                <el-form label-width=80px >
                    <el-form-item label="角色名" >
                        <el-input v-model="roleForm.name" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="角色描述" >
                        <el-input v-model="roleForm.description" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="唯一标识" >
                        <el-input v-model="roleForm.roleFlag" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addRole">确 定</el-button>
                </div>
            </div>
            <div  v-else>
                <el-tree
                        default-expand-all="true"
                        :data="menuTable"
                        show-checkbox
                        node-key="id"
                        ref="tree"
                        :default-checked-keys="checkedKeys"
                        :props="defaultProps">
                    <span class="custom-tree-node" slot-scope="{ node, data }">
                        <i :class="data.icon" class="el_margin_lr"></i>
                        <span>{{ node.label }}</span>
                    </span>
                </el-tree>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="certainAssignPermission">确 定</el-button>
                </div>

            </div>



        </el-dialog>


    </div>

</template>

<script>
    import request from "@/utils/request";

    export default {
        name: "Role",
        props:{
        },
        data(){
            return{
                tableData:{},
                form:{},
                pageIndex:1,
                totalNum:0,
                pageSize:10,
                multipleSelection:[],
                roleName:"",
                dialogFormVisible:false,
                roleForm:{},
                title:"",
                menuTable:[],
                defaultProps:{
                    children: 'children',
                    label: 'description'
                },
                checkedKeys:[],
                currentRoleId:0,
                roleFlag:"",
                currentRoleFlag:"",
            }
        },
        created() {
            this.load();
            request.get("role/queryAllRolesNum",{
                params:{
                    roleName: this.roleName
                }
            }).then(res=>{
                if (res.code === '200'){
                    this.totalNum=res.data;
                }
            });
            this.roleFlag=JSON.parse(localStorage.getItem("user")).roleFlag;
        },
        methods:{
            certainAssignPermission(){
                request.get("roleMenu/deleteAllMenus/"+this.currentRoleId).then(res=>{
                    if (res.code === '200'){
                        this.checkedKeys= this.$refs.tree.getCheckedKeys();
                        request.post("roleMenu/addAllMenus",{
                            roleId:this.currentRoleId,
                            ids:this.checkedKeys,
                        }).then(res=> {
                            if (res.code === '200') {
                                this.dialogFormVisible = false;
                                if (this.currentRoleFlag==='ROLE_ADMIN'){
                                    localStorage.removeItem("user");
                                    this.$router.push("/login");
                                    this.$message.success("请重新登录！");
                                }
                            }
                        });
                    }
                });

            },
            assignPermission(id,val){
                this.title='分配权限';
                this.dialogFormVisible=true;
                this.currentRoleFlag=val;
                request.get("menu/queryAllMenus").then(res=>{
                    if (res.code === '200'){
                        this.menuTable=res.data;
                    }
                });
                request.get("roleMenu/selectAllMenus/"+id).then(res=>{
                    if (res.code === '200'){
                        let ids=[];
                        res.data.forEach(menu=>{
                            if (menu.path!=null || menu.path!==''){
                                ids.push(menu.id);
                            }
                        })
                        this.checkedKeys=ids;
                    }
                });
                this.currentRoleId=id;
            },
            load(){
                request.get("role/queryAllRoles",{
                    params:{
                        pageIndex: this.pageIndex,
                        pageSize: this.pageSize,
                        roleName: this.roleName
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.tableData=res.data;
                        this.totalNum=res.data.length;

                    }
                })
            },
            toEditRole(val){
                this.title='修改角色';
                this.dialogFormVisible=true;
                this.roleForm=val;
            },
            addRole(){
                request.post("role/addRole",this.roleForm).then(res=>{
                    if (res.code=='200'){
                        this.$message({
                            message: '修改用户成功',
                            type: 'success'
                        });
                        this.dialogFormVisible=false;
                        this.load();
                    }else{
                        this.$message.error("修改用户失败")
                    }
                })
            },
            search(){
                this.load();
                request.get("role/queryAllRolesNum",{
                    params:{
                        roleName: this.roleName
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.totalNum=res.data;
                    }
                })
            },
            changeState(id){
                request.get("role/editRoleState",{
                    params:{
                        id : id,
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.load();
                    }
                })
            },
            download(url){
                window.open(url);
            },
            handleSelectionChange(val){
               this.multipleSelection=val;
            },
            batchDeleteUsers(){
                let ids=this.multipleSelection.map(val =>val.id);
                request.post("role/batchDeleteRole",ids).then(res=>{
                    if (res.code === '200'){
                        this.load();
                    }
                })
            },
            handleCurrentChange(val){
                this.pageIndex=val;
                this.load();
            },

            handleSizeChange(val){
                this.pageSize=val;
                this.load();
            },
            deleteRole(id){
                request.get("role/deleteRole",{
                    params:{
                        id : id,
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.load();
                    }
                })

            },
            reset(){
                this.roleName="";
                this.load();
            }
        }
    }
</script>

<style scoped>

</style>
